package com.supersit.GZFDA.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.util.TextUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supersit.GZFDA.R;
import com.supersit.GZFDA.activity.UpLoadActivity;
import com.supersit.GZFDA.entity.UnUpLoad;

public class UnUpLoadDataAdatper extends BaseAdapter implements OnClickListener {
	private List<UnUpLoad> list;
	private Context context;
	private Callback mCallback;
	ViewHolder holder;
	private boolean isBatch = false;
	private Button uploadbBtton;
	private boolean[] checks; // 用于保存checkBox的选择状态

	public UnUpLoadDataAdatper(List<UnUpLoad> list, Context context,
			Callback mCallback) {
		super();
		this.list = list;
		this.context = context;
		this.mCallback = mCallback;
		this.uploadbBtton = UpLoadActivity.btn_batch_upload;
		checks = new boolean[list.size()];

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub

		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_unupload_data, null);
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.data = (TextView) convertView.findViewById(R.id.tv_data);
			holder.upload = (Button) convertView
					.findViewById(R.id.btn_upload_data);
			holder.selected = (CheckBox) convertView
					.findViewById(R.id.cb_selected);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String[] photos = list.get(position).getPhotos().split(",");
		String photoNum = "0";
		if (photos.length == 1) {
			if (TextUtils.isEmpty(photos[0])) {
				photoNum = list.get(position).getBZ();
			} else {
				photoNum = photos.length + "";
			}
		} else {
			photoNum = photos.length + "";
		}
		holder.title.setText(list.get(position).getQYMC());
		holder.data.setText("图片(" + photoNum + ")|"
				+ list.get(position).getCJSJ());
		if (list.get(position).isUpLoad()) {
			holder.upload.setText("删除");
		}
		holder.upload.setOnClickListener(this);
		holder.upload.setTag(position);
		if (isBatch) {
			holder.selected.setVisibility(View.VISIBLE);
			holder.upload.setVisibility(View.GONE);
		} else {
			holder.selected.setVisibility(View.GONE);
			holder.upload.setVisibility(View.VISIBLE);
		}
		final UnUpLoad uu = list.get(position);
		final int pos = position; // pos必须声明为final
		holder.selected
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton button,
							boolean arg1) {
						// TODO Auto-generated method stub
						checks[pos] = arg1;
						uu.setSelected(arg1);
						int count = 0;
						for (int i = 0; i < list.size(); i++) {
							UnUpLoad uu = list.get(i);
							if (uu.isSelected()) {
								count++;
							}
						}
						uploadbBtton.setText("点击上传" + count + "条");
					}
				});
		holder.selected.setChecked(checks[pos]);
		return convertView;
	}

	class ViewHolder {
		TextView title;
		TextView data;
		Button upload;
		CheckBox selected;
	}

	/**
	 * 自定义接口，用于回调按钮点击事件到Activity
	 * 
	 * @author Ivan Xu 2014-11-26
	 */
	public interface Callback {
		public void click(View v);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		mCallback.click(arg0);
	}

	public List<UnUpLoad> getList() {
		return list;
	}

	public void setList(List<UnUpLoad> list) {
		this.list = list;
	}

	public boolean batch() {
		if (isBatch == true) {
			isBatch = false;
		} else {
			isBatch = true;
		}
		return isBatch;

	}

}
